home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapter8 / dlgboxip.c < prev    next >
C/C++ Source or Header  |  1996-01-28  |  7KB  |  237 lines

  1.  
  2. #include <windows.h>  
  3. #include "DlgBoxIP.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. typedef struct
  108. {
  109.    int   nEditOne;
  110.    char  szEditTwo[20];
  111. } DLGDATA, *LPDLGDATA;
  112.  
  113. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  114. {
  115. static LPDLGDATA lpMem;
  116.  
  117.    switch( uMsg )
  118.    {
  119.       case WM_CREATE :
  120.               lpMem = (LPDLGDATA)HeapAlloc( GetProcessHeap(), 
  121.                                             HEAP_ZERO_MEMORY, 
  122.                                             sizeof( DLGDATA ) );
  123.               lpMem->nEditOne = 10;
  124.               strcpy( lpMem->szEditTwo, "Edit Two" );
  125.               break;
  126.  
  127.       case WM_COMMAND :
  128.               switch( LOWORD( wParam ) )
  129.               {
  130.                  case IDM_TEST :
  131.                         {
  132.                            HANDLE hDialog = LoadResource( hInst,
  133.                                             FindResource( hInst, "TestDialog",
  134.                                                           RT_DIALOG ) );
  135.  
  136.                            LPVOID lpDialog = LockResource( hDialog );
  137.                            
  138.                            DialogBoxIndirectParam( hInst, lpDialog, hWnd,
  139.                                                    (DLGPROC)TestDlgProc,
  140.                                                    (LPARAM)lpMem );
  141.                         }
  142.                         break;
  143.  
  144.                  case IDM_ABOUT :
  145.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  146.                         break;
  147.  
  148.                  case IDM_EXIT :
  149.                         DestroyWindow( hWnd );
  150.                         break;
  151.               }
  152.               break;
  153.       
  154.       case WM_DESTROY :
  155.               if ( lpMem )
  156.                  HeapFree( GetProcessHeap(), 0, lpMem );
  157.  
  158.               PostQuitMessage(0);
  159.               break;
  160.  
  161.       default :
  162.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  163.    }
  164.  
  165.    return( 0L );
  166. }
  167.  
  168.  
  169. LRESULT CALLBACK TestDlgProc( HWND hDlg, UINT uMsg, 
  170.                               WPARAM wParam, LPARAM lParam )
  171. {
  172. static LPDLGDATA lpMem;
  173.  
  174.    switch( uMsg )
  175.    {
  176.       case WM_INITDIALOG :
  177.             lpMem = (LPDLGDATA)lParam;
  178.  
  179.             SetDlgItemInt ( hDlg, IDC_EDIT1, lpMem->nEditOne, TRUE );
  180.             SetDlgItemText( hDlg, IDC_EDIT2, lpMem->szEditTwo );
  181.             break;
  182.  
  183.       case WM_COMMAND :
  184.             switch( LOWORD( wParam ) )
  185.             {
  186.                case IDOK :
  187.                       {
  188.                          BOOL bTran;
  189.  
  190.                          lpMem->nEditOne = GetDlgItemInt( hDlg, IDC_EDIT1,
  191.                                                    &bTran, TRUE );
  192.                          GetDlgItemText( hDlg, IDC_EDIT2, lpMem->szEditTwo,
  193.                                          sizeof(lpMem->szEditTwo)-1 );
  194.  
  195.                          EndDialog( hDlg, IDOK );
  196.                       }
  197.                       break;
  198.  
  199.                case IDCANCEL : 
  200.                       EndDialog( hDlg, IDCANCEL );
  201.                       break;
  202.             }
  203.             break;
  204.  
  205.       default :
  206.             return( FALSE );
  207.    }
  208.  
  209.    return( TRUE );
  210. }
  211.  
  212.  
  213.  
  214.  
  215. LRESULT CALLBACK About( HWND hDlg,           
  216.                         UINT message,        
  217.                         WPARAM wParam,       
  218.                         LPARAM lParam)
  219. {
  220.    switch (message) 
  221.    {
  222.        case WM_INITDIALOG: 
  223.                return (TRUE);
  224.  
  225.        case WM_COMMAND:                              
  226.                if (   LOWORD(wParam) == IDOK         
  227.                    || LOWORD(wParam) == IDCANCEL)    
  228.                {
  229.                        EndDialog(hDlg, TRUE);        
  230.                        return (TRUE);
  231.                }
  232.                break;
  233.    }
  234.  
  235.    return (FALSE); 
  236. }
  237.